package cn.sciento.transfer.domain.entity;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.choerodon.mybatis.annotation.ModifyAudit;
import io.choerodon.mybatis.annotation.VersionAudit;
import io.choerodon.mybatis.domain.AuditDomain;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.Date;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hibernate.validator.constraints.NotBlank;
import cn.sciento.boot.platform.lov.annotation.LovValue;
import cn.sciento.starter.keyencrypt.core.Encrypt;

@ApiModel("数据消息消费租户配置")
@VersionAudit
@ModifyAudit
@Table(name = "hdtt_cons_tenant_config")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ConsTenantConfig extends AuditDomain {
  public static final String ENCRYPT_KEY = "hdtt_cons_tenant_config";
  
  public static final String FIELD_CONS_TENANT_CONFIG_ID = "consTenantConfigId";
  
  public static final String FIELD_CONS_DB_CONFIG_ID = "consDbConfigId";
  
  public static final String FIELD_PRODUCER_CONFIG_ID = "producerConfigId";
  
  public static final String FIELD_TENANT_ID = "tenantId";
  
  public static final String FIELD_CONSUMER_OFFSET = "consumerOffset";
  
  public static final String FIELD_PROCESS_TIME = "processTime";
  
  public static final String FIELD_PROCESS_STATUS = "processStatus";
  
  public static final String FIELD_PROCESS_MSG = "processMsg";
  
  @Encrypt
  @ApiModelProperty("表ID，主键")
  @Id
  @GeneratedValue
  private Long consTenantConfigId;
  
  @Encrypt
  @ApiModelProperty("DB配置ID")
  @NotNull
  private Long consDbConfigId;
  
  @Encrypt
  @ApiModelProperty("消息生产配置ID,hdtt_producer_config.producer_config_id")
  @NotNull
  private Long producerConfigId;
  
  @ApiModelProperty("租户ID")
  @NotNull
  private Long tenantId;
  
  @ApiModelProperty("消费消息偏移起始数")
  @NotNull
  private Long consumerOffset;
  
  @ApiModelProperty("处理时间")
  private Date processTime;
  
  @ApiModelProperty("处理状态，参考：HDTT_EVENT_PROCESS_STATUS")
  @NotBlank
  @LovValue(lovCode = "HDTT.EVENT_PROCESS_STATUS", meaningField = "processStatusMeaning")
  private String processStatus;
  
  @ApiModelProperty("初始化处理消息")
  private String processMsg;
  
  @Transient
  private String processStatusMeaning;
  
  @Transient
  private String tenantName;
  
  public void init(Long producerConfigId, Long consDbConfigId) {
    if (this.consumerOffset == null)
      this.consumerOffset = 0L;
    this.producerConfigId = producerConfigId;
    this.consDbConfigId = consDbConfigId;
    setProcessStatus("P");
  }
  
  public static String acquiredTag(ConsTenantConfig consTenantConfig) {
    if (consTenantConfig == null)
      return "null"; 
    return (consTenantConfig.getTenantId() == null) ? "null" : consTenantConfig.getTenantId().toString();
  }
  
  @Transient
  private Boolean initDataSuccessFlag = Boolean.TRUE;
  
  @Transient
  private Boolean offsetChangeFlag = Boolean.FALSE;
  
  public Long getConsTenantConfigId() {
    return this.consTenantConfigId;
  }
  
  public void setConsTenantConfigId(Long consTenantConfigId) {
    this.consTenantConfigId = consTenantConfigId;
  }
  
  public Long getConsDbConfigId() {
    return this.consDbConfigId;
  }
  
  public void setConsDbConfigId(Long consDbConfigId) {
    this.consDbConfigId = consDbConfigId;
  }
  
  public Long getProducerConfigId() {
    return this.producerConfigId;
  }
  
  public void setProducerConfigId(Long producerConfigId) {
    this.producerConfigId = producerConfigId;
  }
  
  public Long getTenantId() {
    return this.tenantId;
  }
  
  public void setTenantId(Long tenantId) {
    this.tenantId = tenantId;
  }
  
  public Long getConsumerOffset() {
    return this.consumerOffset;
  }
  
  public void setConsumerOffset(Long consumerOffset) {
    this.consumerOffset = consumerOffset;
  }
  
  public Date getProcessTime() {
    return this.processTime;
  }
  
  public void setProcessTime(Date processTime) {
    this.processTime = processTime;
  }
  
  public String getProcessStatus() {
    return this.processStatus;
  }
  
  public void setProcessStatus(String processStatus) {
    this.processStatus = processStatus;
  }
  
  public String getProcessMsg() {
    return this.processMsg;
  }
  
  public void setProcessMsg(String processMsg) {
    this.processMsg = processMsg;
  }
  
  public ConsTenantConfig addProcessMsg(String processMsg) {
    this.processMsg += processMsg + "\n";
    return this;
  }
  
  public String getProcessStatusMeaning() {
    return this.processStatusMeaning;
  }
  
  public void setProcessStatusMeaning(String processStatusMeaning) {
    this.processStatusMeaning = processStatusMeaning;
  }
  
  public String getTenantName() {
    return this.tenantName;
  }
  
  public void setTenantName(String tenantName) {
    this.tenantName = tenantName;
  }
  
  public Boolean getInitDataSuccessFlag() {
    return this.initDataSuccessFlag;
  }
  
  public void setInitDataSuccessFlag(Boolean initDataSuccessFlag) {
    this.initDataSuccessFlag = initDataSuccessFlag;
  }
  
  public Boolean getOffsetChangeFlag() {
    return this.offsetChangeFlag;
  }
  
  public ConsTenantConfig setOffsetChangeFlag(Boolean offsetChangeFlag) {
    this.offsetChangeFlag = offsetChangeFlag;
    return this;
  }
  
  public String toString() {
    return (new ToStringBuilder(this))
      .append("consTenantConfigId", this.consTenantConfigId)
      .append("consDbConfigId", this.consDbConfigId)
      .append("producerConfigId", this.producerConfigId)
      .append("tenantId", this.tenantId)
      .append("consumerOffset", this.consumerOffset)
      .append("processTime", this.processTime)
      .append("processStatus", this.processStatus)
      .append("processMsg", this.processMsg)
      .append("processStatusMeaning", this.processStatusMeaning)
      .append("tenantName", this.tenantName)
      .append("initDataSuccessFlag", this.initDataSuccessFlag)
      .append("offsetChangeFlag", this.offsetChangeFlag)
      .toString();
  }
}
